package com.xlf.service.base.service.impl.validate;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xlf.common.auth.util.SecurityUtils;
import com.xlf.common.core.constant.Constants;
import com.xlf.common.core.exception.BizException;
import com.xlf.common.core.util.StringUtils;
import com.xlf.pojo.base.*;
import com.xlf.service.base.mapper.ContestMapper;
import com.xlf.service.base.mapper.ContestRegisterMapper;
import com.xlf.service.base.mapper.LanguageMapper;
import com.xlf.service.base.mapper.ProblemLanguageMapper;
import com.xlf.service.base.pojo.dto.JudgeListReqDTO;
import com.xlf.service.base.pojo.dto.SubmitJudgeDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.nio.file.OpenOption;
import java.util.Date;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/11/11 11:33
 * @Description: TODO 判题校验类
 * @Version 1.0
 */
@Service
public class JudgeValidator {


    @Resource
    ContestRegisterMapper contestRegisterMapper;

    @Resource
    ContestMapper contestMapper;

    @Resource
    LanguageMapper languageMapper;

    @Resource
    ProblemLanguageMapper problemLanguageMapper;


    /**
     * 判题语言校验
     * 1. 校验语言是否存在
     * 2. 校验题目是否支持该语言
     * @param judge
     */
    @NotNull
    public void judgeLanguageValidate(Judge judge){

        if(StringUtils.isEmpty(judge.getLanguage()))
            throw new BizException("提交的语言不能为空！");

        Language language = languageMapper.selectOne(new LambdaQueryWrapper<Language>()
                        .select(Language::getId)
                .eq(Language::getName, judge.getLanguage()));
        if(language == null)
            throw new BizException("不支持该语言！");

        if(judge.getPid() != null){
            if(problemLanguageMapper.selectCount(new LambdaQueryWrapper<ProblemLanguage>()
                    .eq(ProblemLanguage::getLid,language.getId())
                    .eq(ProblemLanguage::getPid,judge.getPid())).equals(0L))
                throw new BizException("题目不支持该语言！");
        }
    }

    /**
     * 提交类型的校验
     * 练习提交：
     * 比赛提交：
     * 1.未报名不能提交
     * 2.比赛不存在不能提交
     * 3.比赛未开始不能提交
     * 4.比赛结束后不能提交
     * @param judge
     */
    @NotNull
    public void submissionTypeValidate(Judge judge){

        Object userId = SecurityUtils.getUserId();

        //练习提交
        if(judge.getCid().equals(0L)){

        //比赛提交
        }else{

            Long count = contestRegisterMapper.selectCount(new LambdaQueryWrapper<ContestRegister>()
                    .eq(ContestRegister::getCid, judge.getCid())
                    .eq(ContestRegister::getUid, userId));
            if(count > 0){
                throw new BizException("未报名,不可提交！");
            }

            Contest contest = contestMapper.selectOne(new LambdaQueryWrapper<Contest>()
                    .select(Contest::getStartTime, Contest::getEndTime)
                    .eq(Contest::getId, judge.getCid())
                    .eq(Contest::getStatus, Constants.NORMAL));

            if(contest == null)
                throw new BizException("该比赛不存在！");
            Date date = new Date();
            if(date.before(contest.getStartTime()))
                throw new BizException("比赛未开始！");
            if(date.after(contest.getEndTime()))
                throw new BizException("比赛已结束！");

        }

    }

    /**
     * 前台获取评测列表校验
     *
     * @param judgeListReqDTO
     */
    @NotNull
    public void getJudgeListValidator(JudgeListReqDTO judgeListReqDTO) {

    }
}
